import { Component,Input, OnInit } from '@angular/core';
import { NziModalService } from 'ng-zorro-iop/modal/nzi-modal.service';
import { NzMessageService } from "ng-zorro-antd";
import { FcsService } from "../../service/fcs.service";
import { LayerCreateComponent } from '../create/layer-create.component';
import { CommonService } from '../../service/common.service';
import { NavHeaderService } from '@global/layout/default/nav-header/nav-header.service';
import { Group, Region } from "../../shared-component/class/instance";
import { ActivatedRoute, Router } from "@angular/router";


@Component({
  selector: 'app-layer-detail',
  templateUrl: './layer-detail.component.html',
  styleUrls: ['./layer-detail.component.less']
})
export class LayerDetailComponent implements OnInit {
    isLoading = true;
    data :any;
    regionName: string;
    currentRegion: Region = {
      regionCode: '',
      regionName: ''
    };
    layerId = "";
    versionData =[];

    pageNo = 1;
  pageSize = 10;
  totalCount = 0;
  
  

  constructor(
    private fcsService: FcsService,
    private modalService: NziModalService,
    private msgService: NzMessageService,
    private router: Router,
    private commonService: CommonService,
    private navHeaderService: NavHeaderService,
    private activatedRoute: ActivatedRoute,
  ) { }

  ngOnInit() {
    this.layerId = this.activatedRoute.snapshot.params["id"];
    this.getLayerDetail();
    
  }

  getLayerDetail() {
    this.fcsService.getLayerDetail(this.layerId).subscribe(res => {
      this.data = res;
      this.versionData = res.versionList;
      // this.totalCount = this.versionData.length;
      // console.log(this.versionData)
      // console.log(res)
      this.regionName = this.data["regionName"];
      this.currentRegion.regionCode = this.data["regionCode"];
      this.currentRegion.regionName = this.data["regionName"];
      this.isLoading = false;
    }, error => {
      this.isLoading = false;
    })
  }


  createLayer() {
    
  }

  createVersion(layerId){
    this.router.navigate(['/layer/update'],{queryParams:{layerId:layerId,regionCode:this.currentRegion.regionCode,regionName:this.currentRegion.regionName}});
  }

  deleteVersion(data){
    this.modalService.delete({
      nzTitle: '删除层',
      nzContentTitle: `确定删除层<em>“${data.layerName}：</em><em>版本${data.version}”</em> 吗？`,
      nzOnOk: () => {
        return new Promise((resolve, reject) => {
          this.fcsService.deleteVersion(data.layerId,data.version).subscribe(
            res => {
              if (res && res.code) {
                resolve(false);
                this.msgService.error(res.message || '删除失败！');
              } else {
                 
                this.msgService.success('删除成功！');
                resolve(true);
             
                if(this.versionData.length < 2 ){
                   this.router.navigate(['/layer']);
                }else{
                  this.getLayerDetail();
                }
                
               
              }
            },
            err => {
              resolve(false);
            });
        });
      },
      nzCancelText: '取消',
      nzOnCancel: () => { },
    });
  }


  downLoad(data){
  
    const id = this.msgService.loading(`正在下载版本 ${data.version} 代码包`, { nzDuration: 0 }).messageId;
    this.fcsService.downLoadCode(data.layerId,data.version).subscribe( res => {
        
        this.msgService.remove(id);
        window.open(res.downloadLink);
    })
     
  }
  getVersionList(){

  }
}
